package be.destin.scheme.search;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import be.destin.skos.core.Concept;
import be.destin.skos.core.Count;
import be.destin.skos.core.ReferringApplication;
import be.destin.skos.core.SchemeInterface;
import be.destin.skos.core.SkosManager;
import be.destin.skos.html.SkosHtmlManager;
import be.destin.skos.search.IndexationCounter;

public class SchemeIndexationCounter extends IndexationCounter {
	
	SkosHtmlManager skosHtmlManager = null;
	SchemeInterface defaultScheme = null;

	public SchemeIndexationCounter (SkosHtmlManager aManager, String anApplication, String aRole) {
        super();
        super.setSkosManager(aManager.getSkosManager());
        skosHtmlManager = aManager;
        if (anApplication != null && !anApplication.isEmpty()) {
        	setApplication(anApplication);
        } else setApplication(SkosHtmlManager.askosiDisplayApplication);
        if (aRole != null && !aRole.isEmpty()) {
        	setRole(aRole);
        }
    }

	@Override
    public Locale getSessionLocale (HttpServletRequest request) {
        return new Locale(skosHtmlManager.getLanguage());
    }

	@Override
    public SchemeInterface getDefaultScheme (String currentIndex) {
        return defaultScheme;
    }

	@Override
    public String getSearchTypeScheme () {
        return SkosManager.aboutRoles;
    }

	@Override
	public Count countUsage(String indexName, String aboutConcept) {
		Concept aConcept = skosHtmlManager.getSkosManager().getScheme_Concept(aboutConcept, getDefaultScheme(indexName), null, null);
		if (aConcept == null) return null;
		ReferringApplication ra = aConcept.getUsage().getReferringApplication(getApplication());
		if (ra == null) return null;
		return ra.getCount(indexName);
	}

    /**
	 * @return the skosHtmlManager
	 */
	public SkosHtmlManager getSkosHtmlManager() {
		return skosHtmlManager;
	}

	/**
	 * @param skosHtmlManager the skosHtmlManager to set
	 */
	public void setSkosHtmlManager(SkosHtmlManager skosHtmlManager) {
		this.skosHtmlManager = skosHtmlManager;
	}

	/**
	 * @return the defaultScheme
	 */
	public SchemeInterface getDefaultScheme() {
		return defaultScheme;
	}

	/**
	 * @param defaultScheme the defaultScheme to set
	 */
	public void setDefaultScheme(SchemeInterface defaultScheme) {
		this.defaultScheme = defaultScheme;
	}

}
